<template>
  <div>
    <a-upload class="fr" name="file" :multiple="true" action="/ats/candidates/applicationAttach/saveAttach" :beforeUpload="beforeUpload" accept=".html, .shtml, .htm, , .doc, .docx, .txt, .zip, .rar, .mht, .xls, .jpg, .jpeg, .bmp, .png, .pdf" :data="uploadData" @change="uploadChange">
      <a-button :loading="loading">上传</a-button>
    </a-upload>
  </div>
</template>
<script>
const typeList = ['image/jpeg', 'text/html', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'text/plain', 'application/x-zip-compressed', 'multipart/related', 'application/vnd.ms-excel', 'image/bmp', 'image/png', 'application/pdf']
export default {
  props: ['candidate'],
  data () {
    return {
      uploadData: {},
      loading: false
    }
  },
  methods: {
    beforeUpload (file) {
      let isCorrectFileType = typeList.includes(file.type)
      if (/\.rar$/.test(file.name)) {
        isCorrectFileType = true
      }
      if (!isCorrectFileType) {
        this.$message.error('请上传正确的文件格式')
        return false
      }
      const isLt100M = file.size / 1024 / 1024 < 100
      if (!isLt100M) {
        this.$message.error('文件必须小于100MB!')
        return false
      }
      this.uploadData.applicationId = this.candidate.applicationId
      this.uploadData.type = 'onboarding'
      console.log(this.uploadData)
      return true
    },
    uploadChange (e) {
      if (e.event && e.event.type === 'progress') {
        this.loading = true
      }
      if (e.file.status === 'done') {
        this.loading = false
        this.$emit('done')
      }
    }
  }
}
</script>
